package com.handinfo.android.ui.window;

import java.io.DataInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;

import android.graphics.Bitmap;

import com.handinfo.android.DWGameManager;
import com.handinfo.android.core.object.Scene.SceneHandler;
import com.handinfo.android.game.GameMessage;
import com.handinfo.android.game.item.Item;
import com.handinfo.android.game.item.ItemEquipment;
import com.handinfo.android.ui.IUIWindows;
import com.handinfo.android.ui.UIWindows;
import com.handinfo.android.uicontrols.DWControlsManager;
import com.handinfo.android.uicontrols.DWListener;
import com.handinfo.android.uicontrols.controls.DWBackground;
import com.handinfo.android.uicontrols.controls.DWButton;
import com.handinfo.android.uicontrols.controls.DWFrame;
import com.handinfo.android.uicontrols.controls.DWImageBox;
import com.handinfo.android.uicontrols.controls.DWLabel;
import com.handinfo.android.uicontrols.controls.DWTitle;
import com.handinfo.android.utils.Tools;

public class UIFixWindow implements IUIWindows
{
	public static final byte TOUCHZOOM = 30;
	private Bitmap img_button;
	private Bitmap img_anxia;
	private Bitmap img_fee;
	private String[] str_button = { "修理装备栏装备", "修理所有装备" };
	private Bitmap img_close;
	private Bitmap img_title;

	private Bitmap fix_background;
	private Bitmap[] fix_img = new Bitmap[6];

	private DWFrame m_fix_frame;
	private DWTitle m_fix_title;
	private DWImageBox m_fix_close;
	private DWButton[] m_fix_fixEquip = new DWButton[2];
	private DWLabel[] m_fix_fee = new DWLabel[3];
	private DWImageBox[] m_fix_img = new DWImageBox[3];

	private int[] fix_img_index = { 5, 0, 1, 2, 3, 4 };

	private void initFrame()
	{
		if (DWControlsManager.getInstance().contains(m_fix_frame))
		{
			DWControlsManager.getInstance().removeControl(m_fix_frame);
			m_fix_frame = null;
		}

		m_fix_frame = new DWFrame(DWFrame.FRAME_TYPE_HALF_HCENTER);
		m_fix_frame.setClickClose(false);

		m_fix_title = new DWTitle("修理装备", m_fix_frame);
		m_fix_title.setBackground(img_title, false);
		m_fix_frame.addControl(m_fix_title);

		DWBackground m_fix_background = new DWBackground(m_fix_frame.getShowWidth() - 40,
				m_fix_frame.getShowWidth() - 40);
		m_fix_background.setNearAnchor(m_fix_frame, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER);
		m_fix_frame.addControl(m_fix_background);

		for (int i = 0; i < 2; i++)
		{
			final int index = i;
			m_fix_fixEquip[i] = new DWButton(str_button[i], img_button);
			m_fix_fixEquip[i].addListener(new DWListener() {
				public void OnDoubleClick()
				{
				}

				public void OnClick()
				{
					if (moneyNum[index] > moneyNum[2])
					{
						DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "金钱不足~");
						return;
					}

					if (moneyNum[index] == 0)
					{
						DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "不需要修理~");
					}
					else
					{
						if (index == 0)
						{
							DWGameManager.getInstance().getSendMessage().sendFixEquips();
						}
						else if (index == 1)
						{
							DWGameManager.getInstance().getSendMessage().sendFixAll();
						}
					}

				}
			});
			m_fix_fixEquip[i].setDownImage(img_anxia);
			m_fix_fixEquip[i].setNearAnchor(m_fix_background, Tools.HCENTER_BOTTOM, Tools.HCENTER_VCENTER, 0,
					i > 0 ? 70 : -70);
			m_fix_frame.addControl(m_fix_fixEquip[i]);

			m_fix_img[i] = new DWImageBox(img_fee);
			m_fix_img[i].setNearAnchor(m_fix_fixEquip[i], Tools.RIGHT_TOP, Tools.RIGHT_BOTTOM, 0, 10);
			m_fix_frame.addControl(m_fix_img[i]);
		}

		m_fix_img[2] = new DWImageBox(img_fee);
		m_fix_img[2].setNearAnchor(m_fix_background, Tools.RIGHT_TOP, Tools.RIGHT_BOTTOM, 0, 10);
		m_fix_frame.addControl(m_fix_img[2]);

		m_fix_close = new DWImageBox(img_close);
		m_fix_close.setNearAnchor(m_fix_title, Tools.RIGHT_VCENTER, Tools.RIGHT_VCENTER);
		m_fix_close.setTouchZoomIn(TOUCHZOOM, TOUCHZOOM);
		m_fix_frame.addControl(m_fix_close);
		m_fix_close.addListener(new DWListener() {
			public void OnDoubleClick()
			{
			}

			public void OnClick()
			{
				close((byte) 0);
			}
		});
	}

	/**
	 * 修改数据
	 */
	private long[] moneyNum = new long[3];

	private void addMoneyNum()
	{

		for (int i = 0; i < 3; i++)
		{
			if (m_fix_fee[i] != null)
			{
				m_fix_frame.removeControl(m_fix_fee[i]);
				m_fix_fee[i] = null;
			}
			m_fix_fee[i] = new DWLabel(UIBag.splitMoney(moneyNum[i]));
			m_fix_fee[i].setBackgroundColor(Tools.ALPHA);
			m_fix_fee[i].setNearAnchor(m_fix_img[i], Tools.RIGHT_VCENTER, Tools.LEFT_VCENTER);
			m_fix_frame.addControl(m_fix_fee[i]);
		}
	}

	public void init()
	{
		img_button = UIWindows.createImage("/img/newui/anniu_12.gnp");
		img_anxia = UIWindows.createImage("/img/newui/anniu_12ax.gnp");
		img_fee = UIWindows.createImage("/img/newui/jinbi_1.gnp");
		img_close = UIWindows.createImage("/img/newui/fanhuianniu_1.gnp");// 关闭窗口图标
		img_title = UIWindows.createImage("/img/newui/zhuangbeixiuli_1.gnp");

		fix_background = UIWindows.createImage("/img/newui/sunhuaidt_1.gnp");
		for (int i = 0; i < fix_img.length; i++)
		{
			fix_img[i] = UIWindows.createImage("/img/newui/sunhuai_" + (i + 1) + ".gnp");
		}
	}

	public void open(byte index)
	{
		if (((SceneHandler) DWGameManager.getInstance().getGameHandler()).m_scene != null)
		{
			if (((SceneHandler) DWGameManager.getInstance().getGameHandler()).m_scene.m_dwDMNPCTaskList != null)
			{
				DWControlsManager.getInstance().removeControl(
						((SceneHandler) DWGameManager.getInstance().getGameHandler()).m_scene.m_dwDMNPCTaskList);
			}
			DWFrame frame = UIWindows.getInstance().m_npcShop.m_npc_shop;
			if (frame != null && DWControlsManager.getInstance().contains(frame))
			{
				UIWindows.getInstance().m_npcShop.close((byte) 0);
			}
		}

		initFrame();
		DWControlsManager.getInstance().addControl(m_fix_frame);
	}

	public void close(byte index)
	{
		DWControlsManager.getInstance().removeControl(m_fix_frame);
		m_fix_frame = null;
		moneyNum[0] = 0;
		moneyNum[1] = 0;
		moneyNum[2] = 0;
	}

	public void dispose()
	{
	}

	public void logic()
	{
	}

	/**
	 * 打开修理界面
	 * @param dis
	 */
	public void recvOpenFixShop(DataInputStream dis)
	{
		try {
			byte state = dis.readByte();
			if (state == 1) {
				open((byte) 0);
				moneyNum[0] = dis.readLong();
				moneyNum[1] = dis.readLong();
				moneyNum[2] = DWGameManager.getInstance().m_role.m_gold;
				addMoneyNum();
			}
		}
		catch (Exception e) {
			Tools.debugPrintln("接收修理界面开启出错");
		}
	}

	/**
	 * 修理装备栏道具
	 * @param dis
	 */
	public void recvFixEquips(DataInputStream dis)
	{
		DWGameManager.getInstance().getSendMessage().unlockNetwork(GameMessage.锁_修理);
		try {
			byte state = dis.readByte();
			switch (state) {
				case 1:
					DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "修理装备栏装备成功");
					moneyNum[0] = 0;
					moneyNum[2] = DWGameManager.getInstance().m_role.m_gold;
					addMoneyNum();
					break;
				default:
					DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "修理装备栏装备失败");
					break;
			}
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 修理所有道具
	 * @param dis
	 */
	public void recvFixAll(DataInputStream dis)
	{
		DWGameManager.getInstance().getSendMessage().unlockNetwork(GameMessage.锁_修理);
		try {
			byte state = dis.readByte();
			switch (state) {
				case 1:
					DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "修理所有道具成功");
					moneyNum[1] = 0;
					moneyNum[2] = DWGameManager.getInstance().m_role.m_gold;
					addMoneyNum();
					break;
				default:
					DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "修理所有道具失败");
					break;
			}
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 耐久度
	 */
	private DWImageBox m_fix_background;
	private DWImageBox[] m_fix_bodyimg;

	public void setDurableMessage()
	{
		if (UIMain.m_Uimain_Frame == null)
			return;

		if (DWGameManager.getInstance().m_role.checkEquipDurable()) {
			if (m_fix_background == null)
			{
				m_fix_background = new DWImageBox(fix_background);
				m_fix_background.setTouchZoomIn(UIFixWindow.TOUCHZOOM, UIFixWindow.TOUCHZOOM);
//			m_fix_background.setShow(false);
				m_fix_background.setNearAnchor(UIMain.m_Uimain_Frame, Tools.LEFT_BOTTOM, Tools.LEFT_BOTTOM, 50, -50);
				m_fix_background.addListener(new DWListener() {
					public void OnDoubleClick()
					{
					}

					public void OnClick()
					{
						DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B,
								"装备耐久为零将失去所有属性，请尽快去凌霄殿神匠处修理");
					}
				});
				UIMain.m_Uimain_Frame.addControl(m_fix_background);

				m_fix_bodyimg = new DWImageBox[6];
				for (int i = 0; i < m_fix_bodyimg.length; i++)
				{
					m_fix_bodyimg[i] = new DWImageBox(fix_img[i]);
					m_fix_bodyimg[i].setTouch(false);
					m_fix_bodyimg[i].setShow(false);
					m_fix_bodyimg[i].setNearAnchor(m_fix_background, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER);
					UIMain.m_Uimain_Frame.addControl(m_fix_bodyimg[i]);
				}
			}
			
			m_fix_background.setShow(true);
			for(int i=0;i<m_fix_bodyimg.length;i++)
			{				
				m_fix_bodyimg[i].setShow(false);
			}
			
			ArrayList<Item> m_fix_list = DWGameManager.getInstance().m_role.getEquipDurableList();
			if (m_fix_list.size() > 0)
			{
				for (int i = 0; i < m_fix_list.size(); i++)
				{
					int index = fix_img_index[((ItemEquipment) m_fix_list.get(i)).m_subtype];
					m_fix_bodyimg[index].setShow(true);
				}
			}

		}
		else
		{
			if(m_fix_background==null || m_fix_bodyimg==null)
				return;
			
			m_fix_background.setShow(false);
			for(int i=0;i<m_fix_bodyimg.length;i++)
			{				
				m_fix_bodyimg[i].setShow(false);
			}
		}
	}

	/**
	 * 装备耗损
	 * @param equip
	 */
	public static void setItemFix(Item item)
	{
		if (item == null || !(item instanceof ItemEquipment) || item.m_jobLimit!=DWGameManager.getInstance().m_role.m_vocation)
			return;

		ItemEquipment equip=(ItemEquipment) item;
		if (equip.m_maxDurable > 0)
		{
			if (equip.m_nowDurable ==0)
			{
				equip.m_isLimit = true;
			}
			else
			{
				equip.m_isLimit = false;
			}
		}
	}
}
